const router = require('koa-router')();
const fs = require("fs")
const path = require("path")

const path_content = path.resolve(__dirname,"../../editor/src/utils/content.json")
console.log("path_content",path_content)

router.post("/changeContent",async (ctx)=>{
  let params = ctx.request.body
  let content = params.data
  let writeFile = ()=>{
    return new Promise((resolve, reject) => {
      fs.writeFile(path_content,JSON.stringify(content),"utf8",(err)=>{
        if(err){return resolve(false)}
        resolve(true)
      }) 
    });
  }
  writeFile.then(e=>{
    console.log(e)
  })
  if(err){return ctx.body = {status:400,msg:err}}
  ctx.body = {status:200,msg:"ok"}
})

module.exports = router